$(document).ready(
		function() {
			
			// ajax query
			$(".button_div .submit[type='submit']").click(function() {
				if(!checkForm()){
					alert('You must fill in all fields correctly');
					return false;
				};
				$.ajax({
					url : '/SummaryTask4/controller',
					type : 'POST',
					cashe : false,
					data : 'command=contactToAdmin&' + $('div#question_block form').serialize(),
					beforeSend : function() {
						alert("Your message will be send. Check your Email");
					}
				});
				$(".modal_bg, .modal_window").hide(600);
				return false;
			});
			
			// checks email
			function validateEmail(email) { 
			    var re = /\S+@\S+\.\S+/;
			    return re.test(email);
			}; 
			
			// checks fields in form
			function checkForm(){
				if($('div#question_block form #name').val() == ''){return false;};
				if($('div#question_block form #surname').val() == ''){return false;};
				if(!validateEmail($('div#question_block form #email').val())){return false;};
				if($('div#question_block form #question').val() == ''){return false;};
				return true;
			};
			
			// modal window
			$('#contact_to_admin').click(
					function() {
						var HeightDocument = $(document).height();
						var WidthDocument = $(document).width();
						var HeightScreen = $(window).height();
						var HeightModal = $(".modal_window").height();
						var TopPosition = Math.abs(Math
								.ceil((HeightScreen - HeightModal) / 2));

						// Imposes a gray background
						$(".modal_bg").css({
							"width" : WidthDocument,
							"height" : HeightDocument
						});
						$(".modal_bg").fadeTo("fast", 0.9);

						$(".modal_window").css({
							"top" : TopPosition + "px"
						}).delay(400).fadeIn('fast');

						// Prohibits to scroll page
						$("body").css({
							"overflow" : "hidden"
						});
						return false;
					});

			// Closes modal window
			$("#close_modal").click(function() {
				$(".modal_bg, .modal_window").hide(600);
			});
		});